package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.school.domain.PacCarouselBanner;
import com.ruoyi.school.service.PacCarouselBannerService;
import com.ruoyi.school.mapper.PacCarouselBannerMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * @author jeremylin
 * @description 针对表【pac_carousel_banner(轮播管理)】的数据库操作Service实现
 * @createDate 2025-06-05 02:35:40
 */
@Service
public class PacCarouselBannerServiceImpl extends ServiceImpl<PacCarouselBannerMapper, PacCarouselBanner>
        implements PacCarouselBannerService {

    @Override
    public List<PacCarouselBanner> getList(PacCarouselBanner pacCarouselBanner) {
        return lambdaQuery().orderByAsc(PacCarouselBanner::getBannerSort).list();
    }

    @Override
    public PacCarouselBanner getDetails(PacCarouselBanner pacCarouselBanner) {
        return lambdaQuery().eq(PacCarouselBanner::getId, pacCarouselBanner.getId()).one();
    }

    @Override
    public boolean add(PacCarouselBanner pacCarouselBanner) {
        if (pacCarouselBanner.getBannerUrl() == null) {
            throw new BaseException("请上传轮播图片");
        }

        if (pacCarouselBanner.getBannerSort() == null) {
            PacCarouselBanner one = lambdaQuery().orderByDesc(PacCarouselBanner::getBannerSort).last("limit 1").one();
            pacCarouselBanner.setBannerSort(one.getBannerSort() + 1);
        }

        return save(pacCarouselBanner);
    }

    @Override
    public boolean edit(PacCarouselBanner pacCarouselBanner) {
        if (pacCarouselBanner.getId() == null) {
            throw new BaseException("请传入要修改的轮播id");
        }
        if (pacCarouselBanner.getBannerUrl() == null) {
            throw new BaseException("请上传轮播图片");
        }

        if (pacCarouselBanner.getBannerSort() == null) {
            PacCarouselBanner one = lambdaQuery().orderByDesc(PacCarouselBanner::getBannerSort).last("limit 1").one();
            if (one != null) {
                if (pacCarouselBanner.getId().equals(one.getId())) {
                    pacCarouselBanner.setBannerSort(one.getBannerSort());
                } else {
                    pacCarouselBanner.setBannerSort(one.getBannerSort() + 1);
                }
            }
        }

        return updateById(pacCarouselBanner);
    }

    @Override
    public boolean delete(PacCarouselBanner pacCarouselBanner) {
        if (pacCarouselBanner.getId() != null) {
            return removeById(pacCarouselBanner.getId());
        }
        return false;
    }

    @Override
    public List<PacCarouselBanner> getAllList(PacCarouselBanner pacCarouselBanner) {
        List<PacCarouselBanner> list = lambdaQuery().orderByAsc(PacCarouselBanner::getBannerSort).list();
        return list;
    }
}




